package com.atguigu.ssyx.cart.controller;

import com.atguigu.ssyx.activity.client.ActivityFeignClient;
import com.atguigu.ssyx.cart.service.CartInfoService;
import com.atguigu.ssyx.common.auth.AuthContextHolder;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.order.CartInfo;
import com.atguigu.ssyx.vo.order.OrderConfirmVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * ClassName: CartApiController
 * Package: com.atguigu.ssyx.cart.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-05
 * Time: 10:38
 */

@Api(tags = "购物车接口")
@RestController
@RequestMapping("/api/cart")
public class CartApiController {
    //cart模块没有操作数据库

    @Autowired
    private CartInfoService cartInfoService;

    @Autowired
    private ActivityFeignClient activityFeignClient;

    //添加商品到购物车
    //不操作数据库 添加到Redis
    //添加内容 当前登录用户Id skuId 商品个数
    @ApiOperation("添加商品到购物车")
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum") Integer skuNum){

        //获取登录用户Id值
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.addToCart(userId,skuId,skuNum);
        return Result.ok(null);
    }


    //根据skuId删除购物车
    @ApiOperation("根据skuId删除购物车")
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable("skuId") Long skuId){
        //操作的都是自己的购物车 所以都是要获取到当前自己的登录Id
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.deleteCart(skuId,userId);
        return Result.ok(null);
    }

    //清空购物车
    @ApiOperation("清空购物车")
    @DeleteMapping("/deleteAllCart")
    public Result deleteAllCart(){
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.deleteAllCart(userId);
        return Result.ok(null);
    }

    //批量删除购物车 skuId
    @ApiOperation("批量删除购物车")
    @DeleteMapping("/batchDeleteCart")
    public Result batchDeleteCart(@RequestBody List<Long> skuIdList){
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.batchDeleteCart(skuIdList,userId);
        return Result.ok(null);
    }


    //购物车列表接口
    @ApiOperation("购物车列表接口")
    @GetMapping("/cartList")
    public Result cartList(){
        //根据用户Id得到 用户购物车内容
        Long userId = AuthContextHolder.getUserId();
        List<CartInfo> cartInfoList = cartInfoService.getCartList(userId);
        return Result.ok(cartInfoList);
    }

    //带优惠卷的购物车
    //换购
    @ApiOperation("带优惠卷的购物车")
    @GetMapping("/activityCartList")
    public Result activityList(){
        Long userId = AuthContextHolder.getUserId();
        List<CartInfo> cartInfoList = cartInfoService.getCartList(userId);
        //远程调用
        OrderConfirmVo orderConfirmVo = activityFeignClient.findCartActivityAndCoupon(cartInfoList,userId);
        return Result.ok(orderConfirmVo);
    }

    //获取购物车选中的商品
    @GetMapping("/inner/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable("userId") Long userId){
        return cartInfoService.getCartCheckedList(userId);
    }

}
